home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 August: Tool Chest / Dev.CD Aug 00 TC Disk 2.toast / pc / sample code / java / creator changer / creator changer project / source / progressdialog.java < prev    next >
Encoding:
Java Source  |  2000-06-23  |  5.4 KB  |  215 lines

  1. import java.awt.*;
  2. import java.awt.event.ActionEvent;
  3. import java.awt.event.ActionListener;
  4.  
  5. public class ProgressDialog extends Dialog
  6. {
  7.     public ProgressDialog(Frame parent, boolean modal, ActionListener listener)
  8.     {
  9.         super(parent, modal);
  10.  
  11.         // This code is automatically generated by Visual Cafe when you add
  12.         // components to the visual environment. It instantiates and initializes
  13.         // the components. To modify the code, only use code syntax that matches
  14.         // what Visual Cafe can generate, or Visual Cafe may be unable to back
  15.         // parse your Java file into its visual environment.
  16.         //{{INIT_CONTROLS
  17.         GridBagLayout gridBagLayout;
  18.         gridBagLayout = new GridBagLayout();
  19.         setLayout(gridBagLayout);
  20.         setVisible(false);
  21.         setSize(341,81);
  22.         setBackground(new Color(-1644826));
  23.         paramLabel = new java.awt.Label("Collecting Files...");
  24.         paramLabel.setBounds(11,2,318,25);
  25.         GridBagConstraints gbc;
  26.         gbc = new GridBagConstraints();
  27.         gbc.gridx = 1;
  28.         gbc.gridy = 0;
  29.         gbc.gridwidth = 3;
  30.         gbc.anchor = GridBagConstraints.NORTHWEST;
  31.         gbc.fill = GridBagConstraints.NONE;
  32.         gbc.insets = new Insets(0,0,0,0);
  33.         ((GridBagLayout)getLayout()).setConstraints(paramLabel, gbc);
  34.         add(paramLabel);
  35.         fileLabel = new java.awt.Label("");
  36.         fileLabel.setBounds(11,27,318,21);
  37.         fileLabel.setFont(new Font("SansSerif", Font.PLAIN, 9));
  38.         gbc = new GridBagConstraints();
  39.         gbc.gridx = 1;
  40.         gbc.gridy = 1;
  41.         gbc.gridwidth = 3;
  42.         gbc.anchor = GridBagConstraints.NORTHWEST;
  43.         gbc.fill = GridBagConstraints.BOTH;
  44.         gbc.insets = new Insets(0,0,0,0);
  45.         ((GridBagLayout)getLayout()).setConstraints(fileLabel, gbc);
  46.         add(fileLabel);
  47.         button1 = new java.awt.Button();
  48.         button1.setLabel("Stop");
  49.         button1.setBounds(271,53,58,20);
  50.         gbc = new GridBagConstraints();
  51.         gbc.gridx = 3;
  52.         gbc.gridy = 3;
  53.         gbc.anchor = GridBagConstraints.EAST;
  54.         gbc.fill = GridBagConstraints.NONE;
  55.         gbc.insets = new Insets(0,0,0,0);
  56.         ((GridBagLayout)getLayout()).setConstraints(button1, gbc);
  57.         add(button1);
  58.         progressBar1 = new ProgressBar();
  59.         progressBar1.setBounds(11,58,250,14);
  60.         gbc = new GridBagConstraints();
  61.         gbc.gridx = 1;
  62.         gbc.gridy = 3;
  63.         gbc.gridwidth = 2;
  64.         gbc.fill = GridBagConstraints.NONE;
  65.         gbc.insets = new Insets(10,0,10,10);
  66.         ((GridBagLayout)getLayout()).setConstraints(progressBar1, gbc);
  67.         add(progressBar1);
  68.         setTitle("Processing...");
  69.         //}}
  70.         setResizable(false);
  71.         setVisible(false);
  72.  
  73.         fileCount = 0;
  74.         fileIndex = 0;
  75.         this.listener = listener;
  76.  
  77.         //{{REGISTER_LISTENERS
  78.         SymWindow aSymWindow = new SymWindow();
  79.         this.addWindowListener(aSymWindow);
  80.         SymAction lSymAction = new SymAction();
  81.         button1.addActionListener(lSymAction);
  82.         //}}
  83.     }
  84.  
  85.     public void setFileCount(int fileCount)
  86.     {
  87.         this.fileCount = fileCount;
  88.     }
  89.     
  90.     public int getFileCount()
  91.     {
  92.         return fileCount;
  93.     }
  94.  
  95.     public void setFileLabel(String label)
  96.     {
  97.         fileLabel.setText(label);
  98.     }
  99.     
  100.     public String getFileLabel()
  101.     {
  102.         return fileLabel.getText();
  103.     }
  104.     
  105.     public void setFileIndex(int fileIndex)
  106.     {
  107.         this.fileIndex = fileIndex;
  108.         
  109.         String tempText = replace("<<number>>", "" + fileIndex, paramString);
  110.         paramLabel.setText(replace("<<total>>", "" + fileCount, tempText));
  111.  
  112.         try
  113.         {
  114.             if (fileIndex > 0)
  115.             {
  116.                 progressBar1.setPercent(fileIndex/(double) fileCount);
  117.             }
  118.             else
  119.             {
  120.                 progressBar1.setPercent(0.0);
  121.             }
  122.         }
  123.         catch (IllegalArgumentException exc) { }
  124.     }
  125.     
  126.     public int getFileIndex()
  127.     {
  128.         return fileIndex;
  129.     }
  130.     
  131.     protected String replace(String what, String with, String source)
  132.     {
  133.         String result = new String(source);
  134.         
  135.         int index = source.indexOf(what);
  136.         while (index >= 0)
  137.         {
  138.             result = (result.substring(0, index) + with + result.substring(index + what.length()));
  139.             index = result.indexOf(what);
  140.         }
  141.         
  142.         return result;
  143.     }
  144.     
  145.     /**
  146.      * Shows or hides the component depending on the boolean flag b.
  147.      * @paramLabel b  if true, show the component; otherwise, hide the component.
  148.      * @see java.awt.Component#isVisible
  149.      */
  150.     public void setVisible(boolean b)
  151.     {
  152.         if(b)
  153.         {
  154.             Dimension sSize = Toolkit.getDefaultToolkit().getScreenSize();
  155.             Dimension dSize = getSize();
  156.     
  157.             setLocation((sSize.width - dSize.width) >> 1, (int)(0.33333333 * sSize.height));
  158.         }
  159.         super.setVisible(b);
  160.     }
  161.  
  162.     public void update(Graphics g)
  163.     {
  164.         paint(g);
  165.     }
  166.     
  167.     //{{DECLARE_CONTROLS
  168.     java.awt.Label paramLabel;
  169.     java.awt.Label fileLabel;
  170.     java.awt.Button button1;
  171.     ProgressBar progressBar1;
  172.     //}}
  173.  
  174.     class SymWindow extends java.awt.event.WindowAdapter
  175.     {
  176.         public void windowClosing(java.awt.event.WindowEvent event)
  177.         {
  178.             Object object = event.getSource();
  179.             if (object == ProgressDialog.this)
  180.                 ProgressDialog_WindowClosing(event);
  181.         }
  182.     }
  183.     
  184.     void ProgressDialog_WindowClosing(java.awt.event.WindowEvent event)
  185.     {
  186.         if (listener != null)
  187.             listener.actionPerformed(new ActionEvent(this, ActionEvent.ACTION_PERFORMED, "Progress Stopped"));
  188.         setVisible(false);
  189.         dispose();
  190.     }
  191.  
  192.     class SymAction implements java.awt.event.ActionListener
  193.     {
  194.         public void actionPerformed(java.awt.event.ActionEvent event)
  195.         {
  196.             Object object = event.getSource();
  197.             if (object == button1)
  198.                 button1_ActionPerformed(event);
  199.         }
  200.     }
  201.  
  202.     void button1_ActionPerformed(java.awt.event.ActionEvent event)
  203.     {
  204.         if (listener != null)
  205.             listener.actionPerformed(new ActionEvent(this, ActionEvent.ACTION_PERFORMED, "Progress Stopped"));
  206.         setVisible(false);
  207.         dispose();
  208.     }
  209.  
  210.     protected ActionListener listener;
  211.     protected int fileCount;
  212.     protected int fileIndex;
  213.     protected String paramString = "Processing file <<number>> of <<total>>:";
  214. }
  215.